﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using popLib.ExtensionClass;
using System.Collections;

namespace popLib.ModelSystem
{
    public partial class ReportManagerModel : Form
    {
        public string ThoiGianBaoCao
        {
            get
            {
                if (dtp_NgayBatDau.Value.Date == dtp_NgayKetThuc.Value.Date)
                    return string.Format("Ngày {0}", dtp_NgayBatDau.Value.ToDMYString());
                return string.Format("Từ ngày {0} đến ngày {1}", dtp_NgayBatDau.Value.ToDMYString(), dtp_NgayKetThuc.Value.ToDMYString());
            }
        }
        public string TieuDe
        {
            get
            {
                if (rd_mon.Checked) return string.Format(" THÁNG {0} NĂM {1}", dtp_mon.Value.Month, dtp_mon.Value.Year);
                else if (rd_quy.Checked) return string.Format(" QUÝ {0} NĂM {1}", nud_quy.Value, dtp_quy.Value.Year);
                else return "";
            }
        }
        public FormAction FormAction { get; set; }
        public Type[] ReportList { get; set; }
        public Hashtable SearchControls { get; set; } //key la index of report, value la SearchBox[] cua report do
        public Hashtable SearchData
        {
            get
            {
                Hashtable searchData = new Hashtable();
                foreach (SearchBox control in flp_inputContainer.Controls)
                {
                    foreach (string key in control.DataValue.Keys)
                    {
                        searchData.Add(key, control.DataValue[key]);
                    }
                }
                return searchData;
            }
        }
        public bool IsShowDialogReport { get; set; } //true show 1 report 1 luc thui, tat di moi lam tip duoc

        public ReportManagerModel(string formTitle, FormAction formAction)
        {
            InitializeComponent();
            IsShowDialogReport = true;
            this.FormAction = formAction;
            UpdateInterface();
            this.Text = formTitle;
            this.Icon = MainFormModel.Instance.AppIconSmall;
            SearchControls = new Hashtable();
        }

        public ReportManagerModel(string formTitle)
        {
            InitializeComponent();
            IsShowDialogReport = true;
            UpdateInterface();
            this.Text = formTitle;
            this.Icon = MainFormModel.Instance.AppIconSmall;
            SearchControls = new Hashtable();
        }

        void InitReport()
        {
            int indexer = 1, counter = 0;
            foreach (var item in ReportList)
            {
                using (ReportViewerModel form = (ReportViewerModel)Activator.CreateInstance(item, null))
                {
                    cbx_LoaiBaoCao.Items.Add((form.MaBC.isValid() ? form.MaBC : string.Format("BC{0:00}", indexer++)) + ": " + form.TenBaoCao.ToLower().UpperFirstLetter());
                    SearchControls.Add(counter, form.SearchControls);
                }

                counter++;
            }

            if (ReportList.Count() > 0)
                cbx_LoaiBaoCao.SelectedIndex = 0;

            UpdateInterface();
        }

        private void btn_review_Click(object sender, EventArgs e)
        {
            if (cbx_LoaiBaoCao.SelectedIndex == -1) return;

            FormLib.showAnimatedLoading(this, "Báo cáo đang được tổng hợp.\r\nXin vui lòng đợi...", true);

            if (IsShowDialogReport)
            {                
                using (dynamic form = Activator.CreateInstance(ReportList[cbx_LoaiBaoCao.SelectedIndex], new object[] { dtp_NgayBatDau.Value.Date, dtp_NgayKetThuc.Value.Date, SearchData }))
                {
                    form.NguoiLapBC = MainFormModel.Instance.CurrentUser.Fullname;
                    form.NgayGioIn = MainFormModel.GetServerDateTime().ToGioNgayString();
                    form.ThoiGianBaoCao = this.ThoiGianBaoCao;
                    form.TieuDe = this.TieuDe;
                    form.IsShowDialog = true;
                    form.Print(true, true);
                }
            }
            else
            {
                dynamic form = Activator.CreateInstance(ReportList[cbx_LoaiBaoCao.SelectedIndex], new object[] { dtp_NgayBatDau.Value.Date, dtp_NgayKetThuc.Value.Date, SearchData });
                form.NguoiLapBC = MainFormModel.Instance.CurrentUser.Fullname;
                form.NgayGioIn = MainFormModel.GetServerDateTime().ToGioNgayString();
                form.ThoiGianBaoCao = this.ThoiGianBaoCao;
                form.TieuDe = this.TieuDe;
                form.IsShowDialog = false;
                form.Print(true, true);
            }

            FormLib.hideLoading(this);
        }

        void UpdateInterface()
        {
            FormLib.UpdateControlTabIndex(flp_container.Controls);
            FormLib.UpdateControlTabIndex(flp_inputContainer.Controls);
            flp_inputContainer.Visible = flp_inputContainer.Controls.Count > 0;
            groupBox1.Height = btn_review.Location.Y + 50;
            groupBox1.Location = new Point((this.Width - groupBox1.Width) / 2, (this.Height - groupBox1.Height) / 2 - 15);
        }

        private void ReportManagerModel_Load(object sender, EventArgs e)
        {
            InitReport();
        }

        private void ReportManagerModel_SizeChanged(object sender, EventArgs e)
        {
            UpdateInterface();
        }

        private void rd_day_CheckedChanged(object sender, EventArgs e)
        {
            UpdateDateTime();
        }

        private void UpdateDateTime()
        {
            dtp_day.Enabled = rd_day.Checked;
            dtp_mon.Enabled = rd_mon.Checked;
            nud_quy.Enabled = dtp_quy.Enabled = rd_quy.Checked;
            dtp_NgayBatDau.Enabled = dtp_NgayKetThuc.Enabled = rd_cus.Checked;

            if (dtp_day.Checked)
            {
                dtp_NgayBatDau.Value = dtp_day.Value;
                dtp_NgayKetThuc.Value = dtp_day.Value;
            }

            if (rd_mon.Checked)
            {
                dtp_NgayBatDau.Value = new DateTime(dtp_mon.Value.Year, dtp_mon.Value.Month, 1);
                dtp_NgayKetThuc.Value = dtp_NgayBatDau.Value.AddMonths(1).AddDays(-1);
            }

            if (rd_quy.Checked)
            {
                int mon = (((int)nud_quy.Value - 1) * 3) + 1;
                dtp_NgayBatDau.Value = new DateTime(dtp_quy.Value.Year, mon, 1);
                dtp_NgayKetThuc.Value = dtp_NgayBatDau.Value.AddMonths(3).AddDays(-1);
            }
        }

        private void cbx_LoaiBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbx_LoaiBaoCao.SelectedIndex == -1) return;
            flp_inputContainer.Controls.Clear();
            SearchBox[] controls = (SearchBox[])SearchControls[cbx_LoaiBaoCao.SelectedIndex];
            if (controls == null || controls.Count() == 0) return;
            flp_inputContainer.Controls.AddRange(controls);
            UpdateInterface();
        }

        private void dtp_day_ValueChanged(object sender, EventArgs e)
        {
            UpdateDateTime();
        }
    }

    public abstract class ReportViewerModel : RDLCReportViewer
    {
        public abstract string NguoiLapBC { get; set; }
        public abstract string ThoiGianBaoCao { get; set; }
        public abstract string NgayGioIn { get; set; }
        public abstract string TieuDe { get; set; }
        public abstract string TenBaoCao { get; set; }
        public abstract string MaBC { get; set; }
        public abstract string TieuDePhu { get; set; }

        public SearchBox[] SearchControls { get; set; }


        public ReportViewerModel(string reportFile)
            : base(reportFile)
        {
            InitDefaultProperties();
            this.Text = TenBaoCao;
        }

        public ReportViewerModel()
            : base("")
        {
            InitDefaultProperties();
            this.Text = TenBaoCao;
        }

        public abstract void InitDefaultProperties();
    }


}

